package com.gite.nouette.admin.implementation.view.module;

import java.util.Date;
import java.util.List;

import com.gite.nouette.admin.contract.controller.module.ICModuleLog;
import com.gite.nouette.admin.contract.data.ILog;
import com.gite.nouette.admin.contract.service.IVisitorData;
import com.gite.nouette.admin.contract.view.IVCommand;
import com.gite.nouette.admin.contract.view.module.IVModuleLog;
import com.gite.nouette.admin.implementation.view.AbstractVModule;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class VModuleLogImpl extends AbstractVModule<ICModuleLog> implements IVModuleLog {

	private ListGrid logGrid;

	public VModuleLogImpl(IVCommand<?>... vCommands) {
		super(vCommands);
		setLogGrid(createLogGrid());
		addItem(getLogGrid());
	}

	@Override
	public void setLogs(List<ILog> logs) {
		LogRecord[] logRecords = new LogRecord[logs.size()];
		int i = 0;
		for (ILog log : logs) {
			logRecords[i] = new LogRecord(log.getId(), log.getMessage(), log.getTimestamp(), log.getUsername());
			i++;
		}
		getLogGrid().setData(logRecords);
	}

	protected ListGrid createLogGrid() {
		ListGrid logGrid = new ListGrid();
		logGrid.setShowAllRecords(true);

		ListGridField idField = new ListGridField("id");
		ListGridField logField = new ListGridField("message");
		ListGridField timestampField = new ListGridField("timestamp");
		ListGridField usernameField = new ListGridField("username");
		logGrid.setFields(new ListGridField[] { idField, logField, timestampField, usernameField });
		logGrid.setCanResizeFields(true);
		logGrid.setData(new LogRecord[] {});

		logGrid.setAlign(Alignment.CENTER);
		logGrid.setLayoutAlign(VerticalAlignment.CENTER);
		logGrid.setWidth100();
		logGrid.setHeight100();
		
		return logGrid;
	}

	public ListGrid getLogGrid() {
		return logGrid;
	}

	public void setLogGrid(ListGrid logGrid) {
		this.logGrid = logGrid;
	}

	public class LogRecord extends ListGridRecord implements ILog {
		public LogRecord(Integer id, String message, Date timestamp, String username) {
			setId(id);
			setMessage(message);
			setTimestamp(timestamp);
			setUsername(username);
		}

		@Override
		public Integer getId() {
			return getAttributeAsInt("id");
		}

		@Override
		public void setId(Integer id) {
			setAttribute("id", id);

		}

		@Override
		public String getMessage() {
			return getAttributeAsString("message");
		}

		@Override
		public void setMessage(String message) {
			setAttribute("message", message);
		}

		@Override
		public Date getTimestamp() {
			return getAttributeAsDate("timestamp");
		}

		@Override
		public void setTimestamp(Date timestamp) {
			setAttribute("timestamp", timestamp);
		}

		@Override
		public String getUsername() {
			return getAttributeAsString("username");
		}

		@Override
		public void setUsername(String username) {
			setAttribute("username", username);
		}

		@Override
		public void accept(IVisitorData visitor) {
			throw new UnsupportedOperationException();
		}

	}
}
